<?php


class WSB_View_Helper_FormSelectPlaceholder extends Zend_View_Helper_FormSelect
{

	/**
	 * @param string $name
	 * @param mixed $value
	 * @param array|string $attribs
	 * @param string $listsep
	 * @return string
	 */
	public function formSelectPlaceholder($name, $value = null, $attribs = null, $listsep = "<br />\n")
	{
		$info = $this->_getInfo($name, $value, $attribs, null, $listsep);
        extract($info); // name, id, value, attribs, options, listsep, disable
        $options = $this->_buildOptions();
      	$element = $this->formSelect($name, $value, $attribs, $options, $listsep);
		return $element;
	}

	/**
	 * @return array
	 */
	private function _buildOptions()
	{
		$parentTemplateId = 0;
        $formValues = Zend_Registry::get('Zend_Form_Values');
		if(!empty($formValues['Cms_Model_DbTable_Template']['pid'])) {
			$parentTemplateId = $formValues['Cms_Model_DbTable_Template']['pid'];
		}
		$pairs = array('' => '');
		if($parentTemplateId > 0) {
			$table = new Cms_Model_DbTable_Template();
			$select = $table->select();
			$select->where('id=?', $parentTemplateId);
			$row = $table->fetchRow($select);
			if($row instanceof Cms_Model_Template) {
				$placeholders = $row->getPlaceholders();
				foreach($placeholders AS $placeholder) {
					$pairs[$placeholder->id] = $placeholder->name;
				}
			}
		}
		return $pairs;
	}

}
